var widgetAPI = new Common.API.Widget();
var tvKey = new Common.API.TVKeyValue();

var Main =
{

};

Main.onLoad = function()
	{  
	    var URL = "http://www.flipkart.com/nokia-lumia-630-dual-sim/p/itmdw52ke4htsjky?pid=MOBDW52BHNVSGFFS&otracker=from-search&srno=t_1&query=lumia+630&ref=3aeb17ad-b140-4392-a0e8-417575dfcc06";
	     
	    if (this.XHRObj != null){	       
	    	this.XHRObj.destroy();
	    }
	    this.XHRObj = new XMLHttpRequest();
	     
	    if (this.XHRObj) {
	        alert("got XHR");
	        this.XHRObj.onreadystatechange = function () {
	            alert("State changed to " + Main.XHRObj.readyState);
	            if (Main.XHRObj.readyState == 4) {
	                alert("got data");
	                Main.recieveData();
	                }
	            };
	        
	        this.XHRObj.open("GET", URL, true);
	        this.XHRObj.send(null);
	    }
	     
	    // Enable key event processing
	    this.enableKeys();
	    widgetAPI.sendReadyEvent();
	};

Main.onUnload = function()
{

};

Main.enableKeys = function()
{
	document.getElementById("anchor").focus();
};

Main.keyDown = function()
{
	var keyCode = event.keyCode;
	alert("Key pressed: " + keyCode);

	switch(keyCode)
	{
		case tvKey.KEY_RETURN:
		case tvKey.KEY_PANEL_RETURN:
			alert("RETURN");
			widgetAPI.sendReturnEvent();
			break;
		case tvKey.KEY_LEFT:
			alert("LEFT");
			break;
		case tvKey.KEY_RIGHT:
			alert("RIGHT");
			break;
		case tvKey.KEY_UP:
			alert("UP");
			break;
		case tvKey.KEY_DOWN:
			alert("DOWN");
			break;
		case tvKey.KEY_ENTER:
		case tvKey.KEY_PANEL_ENTER:
			alert("ENTER");
			break;
		default:
			alert("Unhandled key");
			break;
	}
};

Main.recieveData = function () {
	 
	alert("alerting data...");
	var resp = this.XHRObj.responseText;
	alert(resp);
	document.getElementById("content").innerHTML = resp;
	};
